<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Controllers\Admin\RuleController;

class PrivilegeController extends Controller
{
    //添加角色
    public function roleadd(){
    	$privileges = DB::table('privileges')->get();
    	return view('admin.roleadd',['privileges'=>$privileges]);
    }

    //将新添加的角色写入数据库
    public function roleaddPost(Request $r){
    	$row = array(
    		'rname'=>$r->rname,
    		'pid'=>implode(',', $r->pid)
    		);

    		DB::table('roles')->insertGetId($row);

    	
    	return redirect('admins/rolelist');
    }

    //取出所有角色
    public function rolelist(){
    	RuleController::rule();
    	$roles = DB::table('roles')->get();
    	foreach($roles as $k=>$v ){
    		//切割
    		$temp = explode(',',$v->pid);
    		//数据替换
    		foreach($temp as $key => $per){
    			$data = DB::table('privileges')->select('pname')->where('pid',$per)->first();
    			$temp[$key] = $data-> pname;
    		}
    		$roles[$k]->pid = $temp;
    	};
    	return view('admin.rolelist',['roles'=>$roles]);
    }
}
